
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>析构过程 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="16_Optional_Chaining.html" />
    
    
    <link rel="prev" href="14_Initialization.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >析构过程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6790;&#x6784;&#x8FC7;&#x7A0B;">&#x6790;&#x6784;&#x8FC7;&#x7A0B;</h1>
<p><em>&#x6790;&#x6784;&#x5668;</em>&#x53EA;&#x9002;&#x7528;&#x4E8E;&#x7C7B;&#x7C7B;&#x578B;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x88AB;&#x91CA;&#x653E;&#x4E4B;&#x524D;&#xFF0C;&#x6790;&#x6784;&#x5668;&#x4F1A;&#x88AB;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x3002;&#x6790;&#x6784;&#x5668;&#x7528;&#x5173;&#x952E;&#x5B57; <code>deinit</code> &#x6765;&#x6807;&#x793A;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x6784;&#x9020;&#x5668;&#x8981;&#x7528; <code>init</code> &#x6765;&#x6807;&#x793A;&#x3002;</p>
<h2 id="how-deinitialization-works">&#x6790;&#x6784;&#x8FC7;&#x7A0B;&#x539F;&#x7406; </h2>
<p>Swift &#x4F1A;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x7684;&#x5B9E;&#x4F8B;&#x4EE5;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;&#x5982; <a href="23_Automatic_Reference_Counting.html">&#x81EA;&#x52A8;&#x5F15;&#x7528;&#x8BA1;&#x6570;</a> &#x7AE0;&#x8282;&#x4E2D;&#x6240;&#x8BB2;&#x8FF0;&#xFF0C;Swift &#x901A;&#x8FC7;<em>&#x81EA;&#x52A8;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF08;ARC)</em> &#x5904;&#x7406;&#x5B9E;&#x4F8B;&#x7684;&#x5185;&#x5B58;&#x7BA1;&#x7406;&#x3002;&#x901A;&#x5E38;&#x5F53;&#x4F60;&#x7684;&#x5B9E;&#x4F8B;&#x88AB;&#x91CA;&#x653E;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x624B;&#x52A8;&#x5730;&#x53BB;&#x6E05;&#x7406;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5F53;&#x4F7F;&#x7528;&#x81EA;&#x5DF1;&#x7684;&#x8D44;&#x6E90;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x989D;&#x5916;&#x7684;&#x6E05;&#x7406;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x6765;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x5199;&#x5165;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5728;&#x7C7B;&#x5B9E;&#x4F8B;&#x88AB;&#x91CA;&#x653E;&#x4E4B;&#x524D;&#x624B;&#x52A8;&#x53BB;&#x5173;&#x95ED;&#x8BE5;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x5728;&#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x4E2D;&#xFF0C;&#x6BCF;&#x4E2A;&#x7C7B;&#x6700;&#x591A;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x6790;&#x6784;&#x5668;&#xFF0C;&#x800C;&#x4E14;&#x6790;&#x6784;&#x5668;&#x4E0D;&#x5E26;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#x548C;&#x5706;&#x62EC;&#x53F7;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">deinit</span> {
    <span class="hljs-comment">// &#x6267;&#x884C;&#x6790;&#x6784;&#x8FC7;&#x7A0B;</span>
}
</code></pre>
<p>&#x6790;&#x6784;&#x5668;&#x662F;&#x5728;&#x5B9E;&#x4F8B;&#x91CA;&#x653E;&#x53D1;&#x751F;&#x524D;&#x88AB;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x7684;&#x3002;&#x4F60;&#x4E0D;&#x80FD;&#x4E3B;&#x52A8;&#x8C03;&#x7528;&#x6790;&#x6784;&#x5668;&#x3002;&#x5B50;&#x7C7B;&#x7EE7;&#x627F;&#x4E86;&#x7236;&#x7C7B;&#x7684;&#x6790;&#x6784;&#x5668;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x5B50;&#x7C7B;&#x6790;&#x6784;&#x5668;&#x5B9E;&#x73B0;&#x7684;&#x6700;&#x540E;&#xFF0C;&#x7236;&#x7C7B;&#x7684;&#x6790;&#x6784;&#x5668;&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x3002;&#x5373;&#x4F7F;&#x5B50;&#x7C7B;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x81EA;&#x5DF1;&#x7684;&#x6790;&#x6784;&#x5668;&#xFF0C;&#x7236;&#x7C7B;&#x7684;&#x6790;&#x6784;&#x5668;&#x4E5F;&#x540C;&#x6837;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x56E0;&#x4E3A;&#x76F4;&#x5230;&#x5B9E;&#x4F8B;&#x7684;&#x6790;&#x6784;&#x5668;&#x88AB;&#x8C03;&#x7528;&#x540E;&#xFF0C;&#x5B9E;&#x4F8B;&#x624D;&#x4F1A;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x6240;&#x4EE5;&#x6790;&#x6784;&#x5668;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5B9E;&#x4F8B;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x90A3;&#x4E9B;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x5B83;&#x7684;&#x884C;&#x4E3A;&#xFF08;&#x6BD4;&#x5982;&#x67E5;&#x627E;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x88AB;&#x5173;&#x95ED;&#x7684;&#x6587;&#x4EF6;&#xFF09;&#x3002;</p>
<h2 id="deinitializers-in-action">&#x6790;&#x6784;&#x5668;&#x5B9E;&#x8DF5; </h2>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6790;&#x6784;&#x5668;&#x5B9E;&#x8DF5;&#x7684;&#x4F8B;&#x5B50;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x63CF;&#x8FF0;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6E38;&#x620F;&#xFF0C;&#x8FD9;&#x91CC;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x79CD;&#x65B0;&#x7C7B;&#x578B;&#xFF0C;&#x5206;&#x522B;&#x662F; <code>Bank</code> &#x548C; <code>Player</code>&#x3002;<code>Bank</code> &#x7C7B;&#x7BA1;&#x7406;&#x4E00;&#x79CD;&#x865A;&#x62DF;&#x786C;&#x5E01;&#xFF0C;&#x786E;&#x4FDD;&#x6D41;&#x901A;&#x7684;&#x786C;&#x5E01;&#x6570;&#x91CF;&#x6C38;&#x8FDC;&#x4E0D;&#x53EF;&#x80FD;&#x8D85;&#x8FC7; 10,000&#x3002;&#x5728;&#x6E38;&#x620F;&#x4E2D;&#x6709;&#x4E14;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A; <code>Bank</code> &#x5B58;&#x5728;&#xFF0C;&#x56E0;&#x6B64; <code>Bank</code> &#x7528;&#x7C7B;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x5C5E;&#x6027;&#x548C;&#x7C7B;&#x578B;&#x65B9;&#x6CD5;&#x6765;&#x5B58;&#x50A8;&#x548C;&#x7BA1;&#x7406;&#x5176;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bank</span> </span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">var</span> coinsInBank = <span class="hljs-number">10_000</span>
    <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">distribute</span><span class="hljs-params">(coins numberOfCoinsRequested: Int)</span></span> -&gt; <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">let</span> numberOfCoinsToVend = <span class="hljs-built_in">min</span>(numberOfCoinsRequested, coinsInBank)
        coinsInBank -= numberOfCoinsToVend
        <span class="hljs-keyword">return</span> numberOfCoinsToVend
    }
    <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">receive</span><span class="hljs-params">(coins: Int)</span></span> {
        coinsInBank += coins
    }
}
</code></pre>
<p><code>Bank</code> &#x4F7F;&#x7528; <code>coinsInBank</code> &#x5C5E;&#x6027;&#x6765;&#x8DDF;&#x8E2A;&#x5B83;&#x5F53;&#x524D;&#x62E5;&#x6709;&#x7684;&#x786C;&#x5E01;&#x6570;&#x91CF;&#x3002;<code>Bank</code> &#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;<code>distribute(coins:)</code> &#x548C; <code>receive(coins:)</code>&#xFF0C;&#x5206;&#x522B;&#x7528;&#x6765;&#x5904;&#x7406;&#x786C;&#x5E01;&#x7684;&#x5206;&#x53D1;&#x548C;&#x6536;&#x96C6;&#x3002;</p>
<p><code>distribute(coins:)</code> &#x65B9;&#x6CD5;&#x5728; <code>Bank</code> &#x5BF9;&#x8C61;&#x5206;&#x53D1;&#x786C;&#x5E01;&#x4E4B;&#x524D;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x786C;&#x5E01;&#x3002;&#x5982;&#x679C;&#x786C;&#x5E01;&#x4E0D;&#x8DB3;&#xFF0C;<code>Bank</code> &#x5BF9;&#x8C61;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6BD4;&#x8BF7;&#x6C42;&#x65F6;&#x5C0F;&#x7684;&#x6570;&#x5B57;&#xFF08;&#x5982;&#x679C; <code>Bank</code> &#x5BF9;&#x8C61;&#x4E2D;&#x6CA1;&#x6709;&#x786C;&#x5E01;&#x4E86;&#x5C31;&#x8FD4;&#x56DE; <code>0</code>&#xFF09;&#x3002;&#x6B64;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6574;&#x578B;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x63D0;&#x4F9B;&#x7684;&#x786C;&#x5E01;&#x7684;&#x5B9E;&#x9645;&#x6570;&#x91CF;&#x3002;</p>
<p><code>receive(coins:)</code> &#x65B9;&#x6CD5;&#x53EA;&#x662F;&#x5C06; <code>Bank</code> &#x5B9E;&#x4F8B;&#x63A5;&#x6536;&#x5230;&#x7684;&#x786C;&#x5E01;&#x6570;&#x76EE;&#x52A0;&#x56DE;&#x786C;&#x5E01;&#x5B58;&#x50A8;&#x4E2D;&#x3002;</p>
<p><code>Player</code> &#x7C7B;&#x63CF;&#x8FF0;&#x4E86;&#x6E38;&#x620F;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x73A9;&#x5BB6;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x73A9;&#x5BB6;&#x5728;&#x4EFB;&#x610F;&#x65F6;&#x95F4;&#x90FD;&#x6709;&#x4E00;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x786C;&#x5E01;&#x5B58;&#x50A8;&#x5728;&#x4ED6;&#x4EEC;&#x7684;&#x94B1;&#x5305;&#x4E2D;&#x3002;&#x8FD9;&#x901A;&#x8FC7;&#x73A9;&#x5BB6;&#x7684; <code>coinsInPurse</code> &#x5C5E;&#x6027;&#x6765;&#x8868;&#x793A;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Player</span> </span>{
    <span class="hljs-keyword">var</span> coinsInPurse: <span class="hljs-type">Int</span>
    <span class="hljs-keyword">init</span>(coins: <span class="hljs-type">Int</span>) {
        coinsInPurse = <span class="hljs-type">Bank</span>.distribute(coins: coins)
    }
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">win</span><span class="hljs-params">(coins: Int)</span></span> {
        coinsInPurse += <span class="hljs-type">Bank</span>.distribute(coins: coins)
    }
    <span class="hljs-keyword">deinit</span> {
        <span class="hljs-type">Bank</span>.receive(coins: coinsInPurse)
    }
}
</code></pre>
<p>&#x6BCF;&#x4E2A; <code>Player</code> &#x5B9E;&#x4F8B;&#x5728;&#x521D;&#x59CB;&#x5316;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x90FD;&#x4ECE; <code>Bank</code> &#x5BF9;&#x8C61;&#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x786C;&#x5E01;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x786C;&#x5E01;&#x53EF;&#x7528;&#xFF0C;<code>Player</code> &#x5B9E;&#x4F8B;&#x53EF;&#x80FD;&#x4F1A;&#x6536;&#x5230;&#x6BD4;&#x6307;&#x5B9A;&#x6570;&#x91CF;&#x5C11;&#x7684;&#x786C;&#x5E01;&#x3002;</p>
<p><code>Player</code> &#x7C7B;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A; <code>win(coins:)</code> &#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x4ECE; <code>Bank</code> &#x5BF9;&#x8C61;&#x83B7;&#x53D6;&#x4E00;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x786C;&#x5E01;&#xFF0C;&#x5E76;&#x628A;&#x5B83;&#x4EEC;&#x6DFB;&#x52A0;&#x5230;&#x73A9;&#x5BB6;&#x7684;&#x94B1;&#x5305;&#x3002;<code>Player</code> &#x7C7B;&#x8FD8;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x6790;&#x6784;&#x5668;&#xFF0C;&#x8FD9;&#x4E2A;&#x6790;&#x6784;&#x5668;&#x5728; <code>Player</code> &#x5B9E;&#x4F8B;&#x91CA;&#x653E;&#x524D;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x6790;&#x6784;&#x5668;&#x7684;&#x4F5C;&#x7528;&#x53EA;&#x662F;&#x5C06;&#x73A9;&#x5BB6;&#x7684;&#x6240;&#x6709;&#x786C;&#x5E01;&#x90FD;&#x8FD4;&#x8FD8;&#x7ED9; <code>Bank</code> &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> playerOne: <span class="hljs-type">Player</span>? = <span class="hljs-type">Player</span>(coins: <span class="hljs-number">100</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;A new player has joined the game with <span class="hljs-subst">\(playerOne!.coinsInPurse)</span> coins&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;A new player has joined the game with 100 coins&#x201D;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;There are now <span class="hljs-subst">\(Bank.coinsInBank)</span> coins left in the bank&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;There are now 9900 coins left in the bank&#x201D;</span>
</code></pre>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>Player</code> &#x5B9E;&#x4F8B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5411; <code>Bank</code> &#x5BF9;&#x8C61;&#x7533;&#x8BF7;&#x5F97;&#x5230; 100 &#x4E2A;&#x786C;&#x5E01;&#xFF0C;&#x524D;&#x63D0;&#x662F;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x786C;&#x5E01;&#x53EF;&#x7528;&#x3002;&#x8FD9;&#x4E2A; <code>Player</code> &#x5B9E;&#x4F8B;&#x5B58;&#x50A8;&#x5728;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <code>playerOne</code> &#x7684;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x4E2D;&#x3002;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x73A9;&#x5BB6;&#x53EF;&#x4EE5;&#x968F;&#x65F6;&#x79BB;&#x5F00;&#x6E38;&#x620F;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A;&#x53EF;&#x9009;&#x4F7F;&#x4F60;&#x53EF;&#x4EE5;&#x8FFD;&#x8E2A;&#x73A9;&#x5BB6;&#x5F53;&#x524D;&#x662F;&#x5426;&#x5728;&#x6E38;&#x620F;&#x4E2D;&#x3002;</p>
<p>&#x56E0;&#x4E3A; <code>playerOne</code> &#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x8BBF;&#x95EE;&#x5176; <code>coinsInPurse</code> &#x5C5E;&#x6027;&#x6765;&#x6253;&#x5370;&#x94B1;&#x5305;&#x4E2D;&#x7684;&#x786C;&#x5E01;&#x6570;&#x91CF;&#x548C;&#x8C03;&#x7528; <code>win(coins:)</code> &#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;&#x611F;&#x53F9;&#x53F7;&#xFF08;<code>!</code>&#xFF09;&#x5F3A;&#x5236;&#x89E3;&#x5305;&#xFF1A;</p>
<pre><code class="lang-swift">playerOne!.win(coins: <span class="hljs-number">2_000</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;PlayerOne won 2000 coins &amp; now has <span class="hljs-subst">\(playerOne!.coinsInPurse)</span> coins&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;PlayerOne won 2000 coins &amp; now has 2100 coins&#x201D;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The bank now only has <span class="hljs-subst">\(Bank.coinsInBank)</span> coins left&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;The bank now only has 7900 coins left&#x201D;</span>
</code></pre>
<p>&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x73A9;&#x5BB6;&#x5DF2;&#x7ECF;&#x8D62;&#x5F97;&#x4E86; 2,000 &#x679A;&#x786C;&#x5E01;&#xFF0C;&#x6240;&#x4EE5;&#x73A9;&#x5BB6;&#x7684;&#x94B1;&#x5305;&#x4E2D;&#x73B0;&#x5728;&#x6709; 2,100 &#x679A;&#x786C;&#x5E01;&#xFF0C;&#x800C; <code>Bank</code> &#x5BF9;&#x8C61;&#x53EA;&#x5269;&#x4F59; 7,900 &#x679A;&#x786C;&#x5E01;&#x3002;</p>
<pre><code class="lang-swift">playerOne = <span class="hljs-literal">nil</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;PlayerOne has left the game&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;PlayerOne has left the game&#x201D;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The bank now has <span class="hljs-subst">\(Bank.coinsInBank)</span> coins&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;The bank now has 10000 coins&#x201D;</span>
</code></pre>
<p>&#x73A9;&#x5BB6;&#x73B0;&#x5728;&#x5DF2;&#x7ECF;&#x79BB;&#x5F00;&#x4E86;&#x6E38;&#x620F;&#x3002;&#x8FD9;&#x901A;&#x8FC7;&#x5C06;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684; <code>playerOne</code> &#x53D8;&#x91CF;&#x8BBE;&#x7F6E;&#x4E3A; <code>nil</code> &#x6765;&#x8868;&#x793A;&#xFF0C;&#x610F;&#x5473;&#x7740;&#x201C;&#x6CA1;&#x6709; <code>Player</code> &#x5B9E;&#x4F8B;&#x201D;&#x3002;&#x5F53;&#x8FD9;&#x4E00;&#x5207;&#x53D1;&#x751F;&#x65F6;&#xFF0C;<code>playerOne</code> &#x53D8;&#x91CF;&#x5BF9; <code>Player</code> &#x5B9E;&#x4F8B;&#x7684;&#x5F15;&#x7528;&#x88AB;&#x7834;&#x574F;&#x4E86;&#x3002;&#x6CA1;&#x6709;&#x5176;&#x5B83;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x53D8;&#x91CF;&#x5F15;&#x7528; <code>Player</code> &#x5B9E;&#x4F8B;&#xFF0C;&#x56E0;&#x6B64;&#x8BE5;&#x5B9E;&#x4F8B;&#x4F1A;&#x88AB;&#x91CA;&#x653E;&#xFF0C;&#x4EE5;&#x4FBF;&#x56DE;&#x6536;&#x5185;&#x5B58;&#x3002;&#x5728;&#x8FD9;&#x4E4B;&#x524D;&#xFF0C;&#x8BE5;&#x5B9E;&#x4F8B;&#x7684;&#x6790;&#x6784;&#x5668;&#x88AB;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#xFF0C;&#x73A9;&#x5BB6;&#x7684;&#x786C;&#x5E01;&#x88AB;&#x8FD4;&#x8FD8;&#x7ED9;&#x94F6;&#x884C;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="14_Initialization.html" class="navigation navigation-prev " aria-label="Previous page: 构造过程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="16_Optional_Chaining.html" class="navigation navigation-next " aria-label="Next page: 可选链">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"析构过程","level":"1.3.15","depth":2,"next":{"title":"可选链","level":"1.3.16","depth":2,"path":"chapter2/16_Optional_Chaining.md","ref":"chapter2/16_Optional_Chaining.md","articles":[]},"previous":{"title":"构造过程","level":"1.3.14","depth":2,"path":"chapter2/14_Initialization.md","ref":"chapter2/14_Initialization.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/15_Deinitialization.md","mtime":"2019-07-05T01:01:08.079Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

